<?php

namespace App\Http\Controllers\Admin;

use App\Models\PostMeta;
use App\Repositories\CategoryRepositories;
use App\Repositories\PostsRepositories;
use Illuminate\Http\Request;

class PostsController extends BaseController
{
    public function __construct(PostsRepositories $postsRepositories,CategoryRepositories $categoryRepositories)
    {
        $this->postsRepositories = $postsRepositories;
        $this->categoryRepositories = $categoryRepositories;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $categorys = $this->categoryRepositories->allCategory();
        return view('admin.posts.index',compact('categorys'));
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $categorys = $this->categoryRepositories->allCategory();
        $listmeta = $this->postsRepositories->listMeta();
        //dump($this->postsRepositories->listMeta()->toArray());
        return view('admin.posts.create',compact('categorys','listmeta'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        /*$this->validate($request,[
            'title'  => 'required|string',

        ],[
            'title.required'=>'* 名称必传!',

        ]);*/

        //return $request->all();

        $ret = $this->postsRepositories->save($request->all());

        if($ret)
        {
            return redirect()->route('admin.posts')->with(['success'=>'添加完成']);
        }
        return redirect()->back()->with(['success'=>'系统错误']);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $categorys = $this->categoryRepositories->allCategory();
        $info =$this->postsRepositories->find($id);
        $listmeta = $this->postsRepositories->listMeta();
        //dump($info->toArray());

        $checkArr = [];
        $meta=[];
        if(!empty($info))
        {
            $info->post_tag =implode(',',array_column(object_to_array($info->manyTag),'name'));

            foreach ($info->manyCategory as $key =>$val)
            {
                $checkArr[] = $val->id;
            }

            $meta = json_encode($info->meta);
        }

        //dump($meta);


        return view('admin.posts.edit',compact('categorys','info','checkArr','meta','listmeta'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request,[
            'title'  => 'required|string',

        ],[
            'title.required'=>'* 名称必传!',

        ]);

        $ret = $this->postsRepositories->update($id,$request->all());

        if($ret)
        {
            return redirect()->route('admin.posts')->with(['success'=>'更新成功！']);
        }
        return redirect()->back()->with(['success'=>'系统错误']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy()
    {
       $ids=\request()->input('ids');
       $ret =$this->postsRepositories->delete($ids);
       if($ret)
       {
           return response()->json([
               'code' => 0,
               'msg'   => '删除成功！',
           ]);
       }

    }

    public function data(Request $request)
    {
        $data = $request->all();
        $data['post_type'] = 'post';
        $res = $this->postsRepositories->search($data);

        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => $res['total'],
            'data'  => $res['data']
        ];
        return response()->json($data);
    }

    public function recycle()
    {
        $categorys = $this->categoryRepositories->allCategory();
        return view('admin.posts.recycle',compact('categorys'));
    }

    /**
     * 恢复删除的分类
     *
     * @param $id
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function restore(Request $request)
    {
        $ids =$request->input('ids',0);
        $this->postsRepositories->restore($ids);
        return response()->json([
            'code' => 0,
            'msg'   => '恢复成功！',
        ]);

    }

    /**
     * 彻底删除分类
     *
     * @param $id
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function forceDelete($id)
    {
        Category::onlyTrashed()->find($id)->forceDelete();

        return redirect('admin/category/index');
    }

    public function metaDel(Request $request)
    {
        $id = intval($request->input('id'));
        if($id > 0)
        {
            if(PostMeta::destroy($id))
            {
                return response()->json([
                    'code' => 0,
                    'msg'   => '删除成功！',
                ]);
            }
        }
        return response()->json([
            'code' => -1,
            'msg'   => '删除失败！',
        ]);
    }
}
